package com.jxwifi.cloud.quickcleanserver.app;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.utils.LocationGaoDe;

/**
 * 定时循环的Service（）
 * Created by ${oushuhua} on 2017/6/24.
 */

public class CycleService extends Service {

    /**
     * 每1分钟更新一次数据
     */
    private static final int mONE_MINIUTE = 30 * 1000;

    /**
     * 调用Service都会执行到该方法
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mHandler.sendEmptyMessageDelayed(100, mONE_MINIUTE);
        return super.onStartCommand(intent, flags, startId);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 100) {
                refreshlocation();//接口请求
                mHandler.sendEmptyMessageDelayed(100, mONE_MINIUTE);
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }

    public void refreshlocation() {
        Log.w("oushuhua", "refreshlocation 位置上传 ");
        Params params = new Params();
        params.add("lon", LocationGaoDe.getLongitude());
        params.add("lat", LocationGaoDe.getLatitude());
        AuzHttp.get(UrlPool.UPDATE_REAL_LOCATION, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {

            }

            @Override
            public void onFailure(int errorCode, String msg) {

            }
        }, "");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
